<template>
  <div class="container">
    <div class="searchContainer">
      <el-form :model="formData" label-width="80px" size="mini">
        <el-row>
          <el-col :span="8">
            <el-form-item label="新闻id">
              <el-input v-model="formData.searchId"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item>
              <el-button type="primary" @click="search">搜索</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>

    <div class="dataContainer">
      <!-- <div class="optionContainer">
        <el-button type="primary" size="mini" @click="addNew($event, 'add')"
          >新增</el-button
        >
      </div> -->
      <el-table
        :data="tableData"
        :height="500"
        size="small"
        header-row-class-name="headClass"
        highlight-current-row
        border
      >
        <el-table-column prop="id" label="id"> </el-table-column>
        <el-table-column prop="title" label="标题"> </el-table-column>
        <el-table-column prop="content" label="内容"> </el-table-column>
        <!-- <el-table-column prop="image" label="图片">
          <template slot-scope="scope">
            <img :src="scope.row.image" alt="" />
          </template>
        </el-table-column> -->
        <el-table-column fixed="right" width="100" label="操作">
          <template slot-scope="scope">
            <el-button
              type="text"
              size="mini"
              @click="addNew(scope.row, 'edit')"
              >查看</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>

    <div class="paginationContainer">
      <el-pagination background layout="prev, pager, next" :total="total" @current-change="currentChange">
      </el-pagination>
    </div>

    <el-dialog
      title="新闻详情"
      :visible.sync="showDialog"
      @close="handleClose"
    >
      <el-form
        :model="dialogForm"
        ref="dialogForm"
        label-width="80px"
        label-position="left"
        size="mini"
      >
        <el-row>
          <el-col :span="24">
            <el-form-item label="图片">
              <!-- <el-upload
                :class="{ disabled: uploadDisabled }"
                :limit="1"
                action="/api/fileUpload"
                :auto-upload="false"
                list-type="picture-card"
                :on-success="onSuccess"
                :on-change="handleChange"
                :on-remove="handleRemove"
                :file-list="fileList"
                :http-request="uploadSectionFile"
              >
                <i slot="default" class="el-icon-plus"></i>
              </el-upload> -->
              <img class="imgContent" :src="imgUrl" alt="">
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="标题" prop="title">
              <el-input v-model="dialogForm.title" readonly></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="内容" prop="content">
              <el-input type="textarea" v-model="dialogForm.content" readonly></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <!-- <div class="btn-wrapper">
        <el-button type="primary" size="mini" @click="upLoad">确认</el-button>
      </div> -->
    </el-dialog>
  </div>
</template>

<script>
import {
  searchAllNews,
  addNew,
  editNew,
  searchNewById,
  upLoad,
} from "@/api/api";
export default {
  data() {
    return {
      uploadDisabled: false,
      fileList: [],
      formData: {
        searchId: "",
      },

      imgAddress: "",
      imgUrl: '',
      dialogForm: {
        title: "",
        content: "",
      },
      showDialog: false,
      dialogType: "",
      pageForm: {
        pageSize: 10,
        pageNo: 1,
      },
      tableData: [],
      total: 0,
      selectFile: null,
    };
  },
  created() {
    this.searchAllNews(this.pageForm.pageNo);
  },
  methods: {
      currentChange(val) {
      this.searchAllNews(val);
    },
    edit(imgAddress) {
      console.log(imgAddress);
      let params = {
        id: this.dialogForm.id,
        image: imgAddress,
        title: this.dialogForm.title,
        content: this.dialogForm.content,
      };
      editNew(params)
        .then((res) => {
          console.log(res);
          if (res.code === 200) {
              this.$refs.dialogForm.resetFields()
            this.showDialog = false;
            this.searchAllNews(this.pageForm.pageNo);
          }
        })
        .catch((err) => {
          console.log(err);
        });
    },
    upLoad() {
        console.log(this.selectFile)
      if (this.selectFile) {
        let formData = new FormData();
        formData.append("file", this.selectFile.raw);
        upLoad(formData)
          .then((res) => {
            if (this.dialogType === "add") {
              this.add(res);
            } else {
              this.edit(res);
            }
          })
          .catch((err) => {
            console.log(err);
          });
      } else {
        //   this.edit(res);
        let imgUrl = this.dialogForm.imgAddress
        if (this.dialogType === "add") {
            this.add(imgUrl);
        } else {
            this.edit(imgUrl);
        }
      }
    },
    uploadSectionFile(file) {
      console.log(file);
      console.log(this.fileList);
    },
    addNew(row, type) {
      this.imgUrl = row.image
      this.showDialog = true
      Object.assign(this.dialogForm, row)
    },
    search() {
      if (this.formData.searchId) {
          let params = {
              id: this.formData.searchId
          }
        searchNewById(params)
          .then((res) => {
            if (res.code === 200) {
              this.tableData = [res.detailData];
              this.total = 1;
            }
          })
          .catch((err) => {
            console.log(err);
          });
      } else {
        this.searchAllNews(this.pageForm.pageNo);
      }
    },
    searchAllNews(pageNo) {
      let params = {
        pageSize: this.pageForm.pageSize,
        pageNo: pageNo,
      };
      searchAllNews(params)
        .then((res) => {
          console.log(res);
          if (res.code === 200) {
            this.tableData = res.detailData.records;
            this.total = res.detailData.total;
          }
        })
        .catch((err) => {
          console.log(err);
        });
    },
    add(imgAddress) {
      let params = {
        image: imgAddress,
        title: this.dialogForm.title,
        content: this.dialogForm.content,
      };
      addNew(params)
        .then((res) => {
          console.log(res);
          if (res.code === 200) {
              this.$refs.dialogForm.resetFields()
            this.showDialog = false;
            this.searchAllNews(this.pageForm.pageNo);
          }
        })
        .catch((err) => {
          console.log(err);
        });
    },
    onSuccess(res, file, fileList) {
      this.imgAddress = res;
    },
    handleChange(file, fileList) {
      this.selectFile = file;
      console.log(this.selectFile);
      if (fileList.length >= 1) {
        this.uploadDisabled = true;
      } else {
        this.uploadDisabled = false;
      }
      console.log(this.uploadDisabled);
    },
    handleRemove(file) {
      this.uploadDisabled = false;
    },
    handleClose() {
        console.log(this.fileList)
        this.fileList = []
        this.uploadDisabled = false;
        for(let key in this.dialogForm) {
         this.dialogForm[key] = ''
      }
    }
  },
};
</script>

<style lang="less" scoped>
.searchContainer {
  background-color: #fff;
  margin: 10px 0;
  padding-top: 20px;
}
.dataContainer {
  background-color: #fff;
  padding: 20px;
  .optionContainer {
    margin-bottom: 10px;
    text-align: left;
  }
}
.btn-wrapper {
  width: 100%;
  display: flex;
  justify-content: flex-end;
}
.paginationContainer {
  margin-top: 20px;
  text-align: right;
}
.imgContent {
  width: 200px;
  height: 200px;
}
</style>
<style>
.disabled .el-upload--picture-card {
  display: none !important;
}
</style>